import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from "element-plus";
import {reactive} from "vue";

/**
 * 以角色作为模块划分的依据，将模块划分未游客、管理员、卖家、普通用户、专家（未划分模块）、银行用户（未划分模块）
 * @type {Router}
 */
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: '游客',
            component: () => import('@/views/global-Views/TopBar.vue'),
            meta: {
                requiresAuth: false,
            },
            redirect: '/login',
        },
        {
            path: '/SELLER',
            name: '卖家',
            meta: {
                requiresAuth: true,
                roles: ['SELLER']
            },
            component: () => import('@/views/shop/seller/sellerCenter.vue'),
            redirect: '/SELLER/index',
            children: [

                {path: '/SELLER/index', name: '卖家中心首页', component: () => import('@/views/shop/seller/index.vue')},
                {
                  path: '/SELLER/order',
                  name: '生成订单',
                  component: () => import('@/views/shop/order/createOrder.vue')
                },
                {
                    path: '/SELLER/manageOrder',
                    name: '卖家订单管理',
                    component: () => import('@/views/shop/order/manageOrder.vue')
                },

                {
                    path: '/SELLER/persional',
                    name: '卖家个人中心',
                    component: () => import('@/views/global-Views/UpdateInfo.vue')
                },
                {
                    path: '/SELLER/damageReport',
                    name: '卖家货物报损',
                    component: () => import('@/views/shop/demageReport/damageReport.vue')
                },
                {
                    path: '/SELLER/countStock',
                    name: '库存盘点',
                    component: () => import('@/views/shop/seller/StockTaking.vue')
                },
            ]
        },
        {
            // 独立于各模块，族谱上单开一页
            path: '/ADMIN',
            name: '系统管理员',
            component: () => import('@/views/manager/Manager.vue'),
            meta: {
                requiresAuth: true,
                roles: ['ADMIN'],
            },
            redirect: '/ADMIN/home',
            children: [
                {
                    path: '/ADMIN/updateInfo',
                    name: '管理员更新个人信息',
                    component: () => import('@/views/global-Views/UpdateInfo.vue')
                },
                {
                    path: '/ADMIN/password',
                    name: '管理员修改密码',
                    component: () => import('@/views/global-Views/Password.vue')
                },
                {path: '/ADMIN/home', name: '管理员首页', component: () => import('@/views/manager/Home.vue')},
                {path: '/ADMIN/person', name: '管理员个人信息', component: () => import('@/views/manager/Person.vue')},
                {
                    path: '/ADMIN/empManager',
                    name:'管理员管理员工',
                    component:()=>import('@/views/manager/empManager.vue')
                },
                {
                    path: '/ADMIN/orderManager',
                    name: '管理员管理订单',
                    component: () => import('@/views/manager/order/ManageOrderCondition.vue')
                },
                {
                    path: '/ADMIN/ReviewReturnOrder',
                    name: '管理员审核退货订单',
                    component: () => import('@/views/manager/order/ReviewReturnOrder.vue')
                },
                {
                    path:'/ADMIN/countSaleDataGraph',
                    name: '统计销售数据',
                    component: () => import('@/views/manager/order/SalesGraph.vue')
                },
                {
                    path:'/ADMIN/countStock',
                    name:'库存图表',
                    component:()=>import('@/views/manager/stock/StockGraph.vue')
                },
                {
                    path:'/ADMIN/manageStock',
                    name:'管理员管理库存',
                    component:()=>import('@/views/manager/stock/ProductStock.vue')
                },
                {
                    path:'/ADMIN/stockWarning',
                    name:'库存预警',
                    component:()=>import('@/views/manager/stock/stockWarning.vue')
                },
                {
                    path: '/ADMIN/damageReport',
                    name:'管理员管理报损单',
                    component:()=>import('@/views/manager/damageReport/manageDamageReport.vue')
                }
            ]
        },
        {
            path: '/login',
            name: '登录页面',
            component: () => import('@/views/global-Views/Login.vue'),
        },
        {
            path: '/register',
            name: '注册页面',
            component: () => import('@/views/global-Views/Register.vue')
        },
    ]
})

// 获取localStorage
function getLocalStorage() {
    const data = reactive({
        user: JSON.parse(localStorage.getItem('system-user') || '{}'),
    })
    return data.user
}

// 守护路由
router.beforeEach((to, from, next) => {
    // 检查目标路径是否需要鉴权
    if (to.matched.some(record => record.meta.requiresAuth)) {
        const Role = getLocalStorage().role;
        // 检查用户是否已经登录且已经是所需角色
        if (!Role || !to.meta.roles.includes(Role)) {
            if (!Role) {
                ElMessage('未登录，请先登录！')
                next({path: '/login'})
            } else {
                ElMessage('当前用户无权限')
                next({path: '/'})
            }
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router
